package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.SiteMessages;
import com.example.mall.dto.SiteMessagesDto;
import com.example.mall.mapper.SiteMessagesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SiteMessagesQuery;
import com.example.mall.service.ISiteMessagesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 站内消息表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SiteMessagesServiceImpl extends ServiceImpl<SiteMessagesMapper, SiteMessages> implements ISiteMessagesService {

    @Autowired
    private SiteMessagesMapper siteMessagesMapper;

    @Override
    public PageDTO<SiteMessagesDto> queryPage(SiteMessagesQuery siteMessagesQuery) {
        if (siteMessagesQuery == null) {
            siteMessagesQuery = new SiteMessagesQuery();
        }
        Page<SiteMessages> page = siteMessagesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SiteMessages> cartPage = lambdaQuery()
                .like(siteMessagesQuery.getMessageId()!= null, SiteMessages::getMessageId, siteMessagesQuery.getMessageId())
                .like(siteMessagesQuery.getUserId()!= null, SiteMessages::getUserId, siteMessagesQuery.getUserId())
                .like(siteMessagesQuery.getTypeId()!= null, SiteMessages::getTypeId, siteMessagesQuery.getTypeId())
                .like(siteMessagesQuery.getTitle()!= null, SiteMessages::getTitle, siteMessagesQuery.getTitle())
                .like(siteMessagesQuery.getContent()!= null, SiteMessages::getContent, siteMessagesQuery.getContent())
                .like(siteMessagesQuery.getIsRead()!= null, SiteMessages::getIsRead, siteMessagesQuery.getIsRead())
                .orderByDesc(SiteMessages::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SiteMessagesDto.class);
    }

    @Override
    public SiteMessages querySiteMessagesById(Long messageId) {
        AssertUtil.isNotNull(messageId, "messageId不能为空");
        return siteMessagesMapper.selectById(messageId);
    }

    @Override
    public List<SiteMessages> querySiteMessagesList() {
        return this.list();
    }

    @Override
    public void insert(SiteMessagesDto siteMessagesDto) {
        SiteMessages siteMessages = new SiteMessages();
        CopyUtils.copyProperties(siteMessagesDto, siteMessages);
        siteMessages.setMessageId(CodeGenerateUtils.generateUniqueId());
        siteMessages.setCreateDate(new Date());
        siteMessages.setUpdateDate(new Date());
        siteMessages.setIsRead(BaseConstants.LOSE_STATUS);
        siteMessagesMapper.insert(siteMessages);
    }

    @Override
    public void update(SiteMessagesDto siteMessagesDto) {
        AssertUtil.isNotNull(siteMessagesDto.getMessageId(), "messageId不能为空");
        SiteMessages siteMessages = siteMessagesMapper.selectById(siteMessagesDto.getMessageId());
        AssertUtil.isNotNull(siteMessages, "消息不存在");
        CopyUtils.copyProperties(siteMessagesDto, siteMessages);
        siteMessages.setUpdateDate(new Date());
        siteMessagesMapper.updateById(siteMessages);
    }

    @Override
    public void delete(Long messageId) {
        AssertUtil.isNotNull(messageId, "messageId不能为空");
        SiteMessages siteMessages = siteMessagesMapper.selectById(messageId);
        AssertUtil.isNotNull(siteMessages, "消息不存在");
        siteMessages.setIsRead(BaseConstants.EFFECTIVE_STATUS);
        siteMessages.setUpdateDate(new Date());
        siteMessagesMapper.updateById(siteMessages);
    }

    @Override
    public void deleteBatch(List<Long> messageIds) {
        AssertUtil.isNotNull(messageIds, "messageIds不能为空");
        List<SiteMessages> siteMessages = siteMessagesMapper.selectBatchIds(messageIds);
        AssertUtil.isNotNull(siteMessages, "消息不存在");
        for (SiteMessages siteMessage : siteMessages) {
            siteMessage.setIsRead(BaseConstants.EFFECTIVE_STATUS);
            siteMessage.setUpdateDate(new Date());
            siteMessagesMapper.updateById(siteMessage);
        }
    }
}
